package edu.byui.chat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class ClientInThread extends Thread implements Runnable {
	private ObjectInputStream fromServer;
	private ArrayList<Message> messages;
	private ArrayList<Message> readMessages;

	public ClientInThread(ObjectInputStream inStream, ArrayList<Message> messages, ArrayList<Message> read) {
		super();
		this.fromServer = inStream;
		this.messages = messages;
		this.readMessages = read;
	}

	@SuppressWarnings("unchecked")
	public void run() {
		Message message;
		while (true) {
			try {
				Object obj = this.fromServer.readObject();
				if (obj instanceof Message) {
					message = (Message) obj;
					if (!(message.getFrom().equals("")) && message != null) {
						messages.add(0, message);
						System.out.println("You have a NEW message!");
					}
				}
				else if (obj instanceof ArrayList) {
					this.messages = (ArrayList<Message>) obj;
					if (this.readMessages.equals(this.messages)) {
						System.out.println("You have NEW messages!");
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
